Módulo:Gráfico de audiencias en televisión
This module implements . local contrast_ratio = require('Module:Color contrast')._ratio -------------------------------------------------------------------------------- -- TVRG class -- The main class. -------------------------------------------------------------------------------- local TVRG = {} -- Convert HEX codes to RGB values function TVRG.hex2rgb(hex) hex = hex:gsub('#', ) if #hex 3 then -- #000 format return tonumber("0x"..hex:sub(1,1)..hex:sub(1,1))/256, tonumber("0x"..hex:sub(2,2)..hex:sub(2,2))/256, tonumber("0x"..hex:sub(3,3)..hex:sub(3,3))/256 else -- #000000 format return tonumber("0x"..hex:sub(1,2))/256, tonumber("0x"..hex:sub(3,4))/256, tonumber("0x"..hex:sub(5,6))/256 end end -- Allow usages of cells function TVRG.NACell(frame,text) local cell = mw.html.create('td') local attrMatch = '(%a-*)="(^"*)"' infoParam = frame:expandTemplate{title='Celda',args={'N/A'}} -- Gather styles of and assign to node variable while true do local a,b = string.match(infoParam,attrMatch) if a nil or b nil then break end cell:attr(a,b) infoParam = string.gsub(infoParam,attrMatch,,1) end infoParam = string.gsub(infoParam,'%s*|%s*',,1) cell:wikitext(infoParam) return cell end -- Create the graph and table function TVRG.new(frame,args) args = args or {} -- Variables local timeline = '' local longestseason = -1 local promedio = args.promedio and 1 or 0 local season_title = args'título_temporada' or 'Temp.' local root = mw.html.create('div') :attr('align', 'center') -- Create the timeline -- Number of actual viewer numbers local numberargs = 0 for k,v in pairs(args) do if not string.match(k,'^%d+') and not string.match(v,'^%d\.+') then numberargs = numberargs + 1 end end -- Determine number of seasons local num_seasons = -1 for k,v in pairs(args) do local thisseason = tonumber(string.sub(k,6)) if string.sub(k,1,5) 'color' and thisseason > num_seasons then num_seasons = thisseason end end -- Determine number of episodes local num_episodes -- Subtract promedios if included (they should be equal to the number of seasons) if promedio 1 then num_episodes = numberargs-num_seasons else num_episodes = numberargs end -- Bar width local barwidth if num_episodes < 20 then barwidth = 8 elseif num_episodes >= 20 and num_episodes < 50 then barwidth = 7 elseif num_episodes >= 50 and num_episodes < 80 then barwidth = 6 elseif num_episodes >= 80 then barwidth = 5 end -- Graph width local graphwidth if args.ancho_barra nil then if num_episodes < 20 then graphwidth = num_episodes*13+98 elseif num_episodes >= 20 and num_episodes < 50 then graphwidth = num_episodes*12+98 elseif num_episodes >= 50 and num_episodes < 80 then graphwidth = num_episodes*11+98 elseif num_episodes >= 80 then graphwidth = num_episodes*10+98 end else if num_episodes < 20 then graphwidth = num_episodes*(13+args.ancho_barra-barwidth)+98 elseif num_episodes >= 20 and num_episodes < 50 then graphwidth = num_episodes*(12+args.ancho_barra-barwidth)+98 elseif num_episodes >= 50 and num_episodes < 80 then graphwidth = num_episodes*(11+args.ancho_barra-barwidth)+98 elseif num_episodes >= 80 then graphwidth = num_episodes*(10+args.ancho_barra-barwidth)+98 end end -- Basis parameters timeline = timeline .. "ImageSize = width:" .. (args.ancho or graphwidth) .. " height:" .. (args.altura or 300) .. "\n" timeline = timeline .. "PlotArea = left:50 bottom:70 top:20 right:50\n" timeline = timeline .. "AlignBars = justify\n" timeline = timeline .. "Colors =\n" timeline = timeline .. " id:gray value:gray(0.7)\n" -- Colour and legend variables local season = 1 for season = 1,num_seasons do local r,g,b = TVRG.hex2rgb(args.. season or '#006600') local GraphLegend = season_title .. " " .. season if args.. season then local legendKey = string.sub(args.. season, 0, 1) if type(tonumber(legendKey)) "number" then GraphLegend = season_title .. " " .. args.. season else GraphLegend = args.. season end end timeline = timeline .. " id:season" .. season .. " value:rgb("..r..","..g..","..b..") " .. (args.hidelegend nil and "legend:" .. string.gsub(string.gsub(GraphLegend, ' ', '_'), "(.-)", '%1') or '') .. "\n" season = season + 1 end -- Determine maximum viewer figure local maxviewers = -1 local multiple = 'millones' for k,v in pairs(args) do local num = tonumber(v) if tonumber(k) ~= nil and num ~= nil and num > maxviewers then maxviewers = num end end if maxviewers <= 1.5 then multiple = 'miles' maxviewers = maxviewers*1000 for k, v in pairs(args) do local num = tonumber(v) if tonumber(k) ~= nil and num ~= nil then argsk = tostring(num*1000) end end end -- Further parameters, with rounded-up viewer figures as maximum period timeline = timeline .. "DateFormat = x.y\n" timeline = timeline .. "Period = from:0 till:" .. math.ceil(maxviewers) .. "\n" timeline = timeline .. "TimeAxis = orientation:vertical\n" timeline = timeline .. "ScaleMajor = gridcolor:gray increment:" .. 10^math.ceil(math.log10(maxviewers)-math.log10(15)) .. " start:0\n" timeline = timeline .. (args.hidelegend nil and "Legend = orientation:horizontal\n" or "") -- Interval parameter set to prevent overlapping bars local bar = 1 if args.intervalos then timeline = timeline .. "BarData =\n" for k,v in pairs(args) do if string.lower(v) 'n/a' then v = '' end if tonumber(k) ~= nil and (tonumber(v) ~= nil or v '') and (promedio 0 or (promedio 1 and argsk+1 ~= '-' and argsk+1 ~= nil)) then timeline = timeline .. " bar:"..bar.." text:"..((bar 1 or bar % args.intervalos 0) and bar or ' ').."\n" bar = bar + 1 end end end -- Plot data timeline = timeline .. "PlotData =\n" timeline = timeline .. " width:" .. (args.ancho_barra or barwidth) .. "\n" -- Add bars to timeline, one per viewer figure local bar = 1 local season = 0 local thisseason = 0 for k,v in pairs(args) do if string.lower(v) 'n/a' then v = '' end if tonumber(k) ~= nil then if v '-' then -- Hyphen means new season, so change season colour season = season + 1 -- Determine highest number of episodes in a season if thisseason > longestseason then longestseason = thisseason end thisseason = 0 elseif promedio 0 or (promedio 1 and argsk+1 ~= '-' and argsk+1 ~= nil) then -- Include white/black bar to act as border local black_cr = contrast_ratio{args.. season, 'white', 'error' = 0} local white_cr = contrast_ratio{'white', args.. season, 'error' = 0} local bgrnd_cr = (black_cr > white_cr and 'black' or 'gray') timeline = timeline .. " mark:(line," .. bgrnd_cr .. ")\n" timeline = timeline .. " color:" .. bgrnd_cr .. "\n" timeline = timeline .. " bar:" .. bar .. " width:" .. ((args.ancho_barra or barwidth)+2) .. " from:start till:" .. (v ~= '' and v or 'start') .. "\n" -- Include bar for viewer figure, do not include if promedios are included and the next parameter is a new season marker timeline = timeline .. " color:season" .. season .. "\n" timeline = timeline .. " bar:" .. bar .. " from:start till:" .. (v ~= '' and v or 'start') .. "\n" -- Increment tracking variables thisseason = thisseason + 1 bar = bar + 1 end end end -- Determine highest number of episodes in a season after final season's bars if thisseason > longestseason then longestseason = thisseason end -- Etiquetas de los ejes local countryDisplayUS, countryDisplayUK, countryDisplayOther if args'país' ~= nil and args'país' ~= '' then if args'país' "U.S." or args'país' "US" or args'país' "EE.UU." or args'país' "Estados Unidos" then countryDisplayUS = 'Estados Unidos' elseif args'país' "U.K." or args'país' "UK" or args'país' "Reino Unido" then countryDisplayUK = 'Reino Unido' else countryDisplayOther = args'país' end end timeline = timeline .. "TextData =\n" timeline = timeline .. " pos:(" .. ((args.ancho or graphwidth)/2-18) .. ",45) textcolor:black fontsize:XS text:Episodio\n" timeline = timeline .. " pos:(10," .. ((args.altura or 300)-10) .. ") textcolor:black fontsize:XS text:" .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) or "") .. ((countryDisplayUS or countryDisplayUK or countryDisplayOther) and "-A") .. "udiencia (" .. multiple .. ")\n" -- Si hay un título, agréguelo con el título de los espectadores; de lo contrario, solo se mostrará el título de la audiencia. if args'título' ~= nil and args'título' ~= '' then root:wikitext("" .. args'título' .. "'': Audiencia " .. (((countryDisplayUS or countryDisplayUK or countryDisplayOther) and ("en " .. (countryDisplayUS or countryDisplayUK or countryDisplayOther) .. " ")) or "") .. "(" .. multiple .. ")") else root:wikitext("Audiencia por episodio (" .. multiple .. ")") end -- Add timeline to div if args.graf_no nil then root:node(frame:preprocess(' ')) end -- Create ratings table if args.tabla_no nil then local rtable = mw.html.create('table') :addClass('wikitable') :css('text-align', 'center') -- Create headers rows local row = rtable:tag('tr') row:tag('th'):wikitext(season_title) :attr('colspan','2') :attr('rowspan','2') :css('padding-left', '.8em') :css('padding-right', '.8em') row:tag('th') :attr('colspan',longestseason) :wikitext("Episodio número") :css('padding-left', '.8em') :css('padding-right', '.8em') -- promedio column if promedio 1 then row:tag('th') :attr('scope','col') :attr('rowspan','2') :wikitext("Promedio") :css('padding-left', '.8em') :css('padding-right', '.8em') end local row = rtable:tag('tr') for i = 1,longestseason do row:tag('th') :attr('scope','col') :wikitext(i) end local season = 1 local thisseason = 0 -- Create table rows and cells for k,v in pairs(args) do if tonumber(k) ~= nil then -- New season marker, or final episode rating if v '-' or (promedio 1 and argsk+1 nil) then if season > 1 then -- Spanning empty cells with if thisseason < longestseason then row:node(TVRG.NACell(frame,"N/A"):attr('colspan',longestseason-thisseason)) end if promedio 1 then -- If promedios included, then set the promedios cell with value or TBD if v ~= '' then row:tag('td'):wikitext(argsk+1 ~= nil and argsk-1 or v) else row:node(TVRG.NACell(frame,"TBD")) end thisseason = thisseason + 1 end end -- New season marker if v '-' then -- New row with default or preset caption row = rtable:tag('tr') row:tag('th') :css('background-color', args.. season) :css('width','10px') row:tag('th') :attr('scope','row') :wikitext(args.. season and args.. season or season) thisseason = 0 season = season + 1 end elseif promedio 0 or (promedio 1 and argsk+1 ~= '-' and argsk+1 ~= nil) then -- Viewer figures, either as a number or TBD if string.lower(v) 'n/a' then row:node(TVRG.NACell(frame,"N/A")) elseif v ~= '' then row:tag('td'):wikitext(v) :css('width', '35px') else row:node(TVRG.NACell(frame,"TBD")) end thisseason = thisseason + 1 end end end -- Finish by checking if final row needs cells if promedio 0 and thisseason < longestseason then row:node(TVRG.NACell(frame,"N/A"):attr('colspan',longestseason-thisseason)) end -- Add table to div root and return root:node(rtable) end local span = mw.html.create( 'span' ) span:css( 'color', 'red' ) :wikitext(frame:expandTemplate{ title='referencia de broma', args={ 'cita requerida', txtital = 'y' } }) if countryDisplayUS then root:wikitext("Medición de audiencia realizada por Nielsen Media Research." .. (args.refs or tostring(span))) elseif countryDisplayUK then root:wikitext("Medición de audiencia realizada por Broadcasters' Audience Research Board." .. (args.refs or tostring(span))) else root:wikitext("Fuente: " .. (args.refs or tostring(span))) end return tostring(root) end -------------------------------------------------------------------------------- -- Exports -------------------------------------------------------------------------------- local p = {} function p.main(frame) local args = require('Module:Arguments').getArgs(frame, { removeBlanks = false, wrappers = 'Plantilla:Gráfico de audiencias en televisión' }) return TVRG.new(frame,args) end return p